﻿$(document).ready(function() {
prepareInput();
});
function prepareInput() {
	$(this).parent().children("div.hint").children("span.error-validator").remove();
    $('input[type=text], input[type=password],textarea.text, select').focus(function () {       
        $(this).parent().children("div.hint").children("span.valid-email").remove();
        $(this).parent().children("div.hint").children("span.invalid-email").remove();
        where = $(this).parent().children("div.hint");
        if ($(this).parent().children("span.error-validator") && where.length > 0) {
            $(this).parent().children("span.error-validator").clone().prependTo(where);
            $(this).parent().children("span.error-validator").fadeOut();
        }
        if ($(this).parent().children("span.valid-email")) {
            $(this).parent().children("span.valid-email").clone().prependTo(where);
        }

        if ($(this).parent().children("span.invalid-email")) {
            $(this).parent().children("span.invalid-email").clone().prependTo(where);
        }
        where.fadeIn();
        active_hint = $(this).parent().children("div.hint");
    });

    $('input[type=text], input[type=password],textarea.text, select').blur(function() {
        $(this).parent().children("div.hint").fadeOut();
        $(this).parent().children("div.hint").children("span.error-validator").remove();
        $(this).parent().children("div.hint").children("span.valid-email").remove();
        $(this).parent().children("div.hint").children("span.invalid-email").remove();
        active_hint = false;
    });
}

function validateColor(txt) {
    var id = $(txt).attr("id");
    var find = false;
    $.each(Page_Validators, function() {
        if (this.controltovalidate == id) {
            if (!this.isvalid) {
                $(txt).parent().addClass("error-input");
                find = true;
            }
        }
        if (!find) { $(txt).parent().removeClass("error-input"); }
    });
}

$.fn.passwordStrength = function(options) {
    return this.each(function() {
        var that = this; that.opts = {};
        that.opts = $.extend({}, $.fn.passwordStrength.defaults, options);
        that.div = $(that.opts.targetDiv);
        that.defaultClass = that.div.attr('class');
        that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;
        v = $(this).keyup(function() {
            if (typeof el == "undefined")
                this.el = $(this);
            var s = getPasswordStrength2(this.value);
            var p = this.percents;
            var t = Math.floor(s / p);
            if (100 <= s)
                t = this.opts.classes.length - 1;
            this.div.removeAttr('class').addClass(this.defaultClass);
            if (s > 0) { this.div.addClass(this.opts.classes[t]); }
        });
    });

    function getPasswordStrength2(H) {
        var numberRegex = new RegExp("([0-9]+)");
        var smallsymbolRegex = new RegExp("([a-z]+)");
        var largesymbolRegex = new RegExp("([A-Z]+)");
        var specialsymbolRegex = new RegExp("([^A-Za-z0-9]+)");
        var power = 0;
        if (numberRegex.test(H)) { power += 15; }
        if (smallsymbolRegex.test(H)) { power += 15; }
        if (largesymbolRegex.test(H)) { power += 15; }
        if (specialsymbolRegex.test(H)) { power += 15; }
        var l = Math.floor(H.length / 2);
        if (l > 4) { l = 4; }
        power += l * 10;
        if (H.length < 8) { if (power > 85) { power = 85; } }
        return power;
    }
};
$.fn.passwordStrength.defaults = {
    classes: Array('is10', 'is20', 'is30', 'is40', 'is50', 'is60', 'is70', 'is80', 'is90', 'is100'),
    targetDiv: '#dvPasswordStrength',
    cache: {}
}